package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.uuid.IdUtils;
import com.qijian.maindata.domain.BomMaterialPre;
import com.qijian.maindata.domain.query.BomMaterialPreDetailQuery;
import com.qijian.maindata.mapper.BomMaterialPreMapper;
import com.qijian.maindata.service.IBomMaterialPreDetailService;
import com.qijian.maindata.service.IBomMaterialPreService;
import com.qijian.tool.service.IStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 生产BOM用料预算单据Service业务层处理
 *
 * @author qijian
 * @date 2022-05-17
 */
@Service
@RequiredArgsConstructor
public class BomMaterialPreServiceImpl extends ServiceImpl<BomMaterialPreMapper, BomMaterialPre> implements IBomMaterialPreService {

    private final BomMaterialPreMapper bomMaterialPreMapper;
    private final IBomMaterialPreDetailService bomMaterialPreDetailService;
    private final IStorageService storageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdateVo(BomMaterialPre bomMaterialPre, Boolean isAdd) {
        if (isAdd) {
            bomMaterialPre.setId(IdUtils.singletonSnowId());
        }

        // 绑定表头ID-明细
        Set<Long> detailIds = new HashSet<>(bomMaterialPre.getDetails().size());
        bomMaterialPre.getDetails().forEach(detail -> {
            if (ObjectUtils.isNotNull(detail.getId())) {
                detailIds.add(detail.getId());
            }
            detail.setMainId(bomMaterialPre.getId());
        });
        // 绑定表头ID-图片
        List<Long> fileIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(bomMaterialPre.getFiles())) {
            bomMaterialPre.getFiles().forEach(file -> {
                fileIds.add(file.getStorageId());
                file.setClassId(bomMaterialPre.getId());
                file.setClassTable(BomMaterialPre.class.getSimpleName());
            });
        }

        if (!isAdd) {
            // 处理删除图片
            if (CollectionUtil.isNotEmpty(fileIds)) {
                storageService.remove(Collections.singletonList(bomMaterialPre.getId()), fileIds);
            }
            // 处理删除明细
            BomMaterialPreDetailQuery detailQuery = new BomMaterialPreDetailQuery();
            detailQuery.setMainId(bomMaterialPre.getId());
            detailQuery.setIdsNot(detailIds);
        }

        // 保存明细
        if (CollectionUtil.isNotEmpty(bomMaterialPre.getDetails())) {
            bomMaterialPreDetailService.saveOrUpdateBatch(bomMaterialPre.getDetails());
        }
        // 保存图片
        if (CollectionUtil.isNotEmpty(bomMaterialPre.getFiles())) {
            storageService.saveOrUpdateBatch(bomMaterialPre.getFiles());
        }
        return this.saveOrUpdate(bomMaterialPre);
    }

    @Override
    public BomMaterialPre selectVoById(Long id) {
        return bomMaterialPreMapper.selectVoById(id);
    }
}
